package com.ordersystem.controller;

import com.ordersystem.domain.Result;
import com.ordersystem.domain.UpdateUserImg;
import com.ordersystem.domain.User;
import com.ordersystem.service.UserService;
import com.ordersystem.utils.PageUtils;
import com.ordersystem.utils.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : UserController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/20 18:26]
 * @updateUser : asus1020
 * @updateTime : [2022/3/20 18:26]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    private PageUtils pageUtils=new PageUtils();
    @Autowired
    private UploadFileUtils uploadFileUtils;
    @PostMapping("/login")//登录
    public Result login(@RequestBody User user){
        User login = userService.login(user);
        if (login!=null){
            login.setPassword(null);
            return new Result( 200,"登录成功",login);
        }else {
            return new Result(400,"用户名或密码错误");
        }
    }
    @PostMapping("/regist")//注册
    public Result regist(@RequestBody User user){
        Result result = new Result();
        int total = userService.regist(user);
        if (total>0){
            result.setStatus(201);
            result.setMessage("注册成功");
        }else {
            result.setStatus(422);
            result.setMessage("注册失败");
        }
            return result;
    }
    @GetMapping("/checkUsername")//检查用户名是否可用
    public Result checkUsername(String username){
        int total = userService.checkUsernameIsAvailable(username);
        if (total==0){
            return new Result(200,"用户名可用");
        }else {
            return new Result(415,"用户名不可用");
        }

    }
    @GetMapping("/users")//查询用户列表
    public Result getUsers(String query, Integer pagenum, Integer pagesize){
        Map<String, Object> users = userService.findAllUsers(pagenum, pagesize, query);
        if (users!=null){
            return new Result(200,"获取用户列表成功",users);
        }else {
            return new Result(415,"获取用户列表失败");
        }
    }
    @PutMapping("/user/{uid}/state/{type}")
    public Result updateUserState(@PathVariable("uid") Integer uid, @PathVariable("type") boolean state){
        int total = userService.updateUserState(uid, state);
        if (total>0){
            User user = userService.findUserById(uid);
            return new Result(200,"修改成功",user);
        }else {
            return new Result(415,"修改失败");
        }

    }
    @GetMapping("/user/{uid}")
    public Result getUserById(@PathVariable("uid") Integer uid){
        User user = userService.findUserById(uid);
        if (user!=null){
            return new Result(200,"查询成功",user);
        }else {
            return new Result(415,"查询失败");
        }
    }
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        User updateUser = userService.updateUser(user);
        if (updateUser!=null){
            return new Result(200,"修改成功",updateUser);
        }else {
            return new Result(500,"修改失败");
        }

    }
    @DeleteMapping("/deleteUser/{uid}")
    public Result deleteUserById(@PathVariable("uid") Integer uid){
        int total = userService.deleteUserById(uid);
        if (total>0){
            return new Result(204,"删除成功");
        }else {
            return new Result(500,"删除失败");
        }

    }
    //更新用户头像
    @PostMapping("/uploadUserHeaderImg")
    public Result uploadUserHeaderImg(@RequestBody UpdateUserImg updateUserImg) throws IOException {

        Result result = new Result();
        int total = userService.saveUserHeaderImg
                (updateUserImg.getImageUrl(), updateUserImg.getUid());
        if (total>0){
            result.setStatus(200);
            result.setMessage("上传用户头像成功");
        }else {
            result.setStatus(500);
            result.setMessage("上传用户头像失败");
        }

        return result;
    }

}
